package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 分页查询护理计划列表
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    public PageResponse<NursingPlanVo> queryAll(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.queryAll(name, status);
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 添加护理计划
     * @param nursingPlanDto
     */
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        //对象拷贝
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        //设置
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setCreateBy(10L);
        nursingPlan.setUpdateBy(10L);
        nursingPlan.setStatus(0);
        //护理计划表添加
        nursingPlanMapper.add(nursingPlan);
        Long id = nursingPlan.getId();
        //取出projectPlans
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        //给projectPlans每个id设值
        projectPlans.forEach(projectPlan ->{
            projectPlan.setPlanId(id);
        });
        //循环插入计划项目
        nursingProjectPlanMapper.addProjectPlan(projectPlans);
    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Transactional
    public NursingPlanVo getById(Long id) {
        //根据id查询护理项目
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        //根据id查询护理计划项目
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingProjectPlanMapper.getById(id);
        if (nursingProjectPlanVoList != null){
            //给nursingPlanVo的projectPlans设值
            nursingPlanVo.setProjectPlans(nursingProjectPlanVoList);
        }
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     * @param nursingPlanDto
     */
    @Transactional
    public void updateById(Long id,NursingPlanDto nursingPlanDto) {
        //查询是否关联护理等级表
        nursingPlanMapper.updateById(nursingPlanDto);
        //删除护理计划项目
        nursingProjectPlanMapper.deleteById(id);
        //根据id查询护理计划项目
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingProjectPlanMapper.getById(id);
        //判断projectPlans是否为空
        if (nursingProjectPlanVoList != null){
            List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
            projectPlans.forEach(projectPlan ->{
                projectPlan.setPlanId(id);
            });
            //循环插入计划项目
            nursingProjectPlanMapper.addProjectPlan(projectPlans);
        }
    }

    /**
     * 删除护理计划项目
     * @param id
     */
    @Transactional
    public void deleteById(Long id) {
        //状态为1的不能删除
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        if (nursingPlan.getStatus()==1) {
            throw new RuntimeException("状态启用中,不能删除!");
        }
        //根据id删除护理计划
        nursingPlanMapper.deleteById(id);

        //根据id查询护理计划项目
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingProjectPlanMapper.getById(id);
        //判断是否为空
        if (nursingProjectPlanVoList != null){
            //删除护理计划项目
            nursingProjectPlanMapper.deleteById(id);
        }
    }

    /**
     * 修改护理计划状态
     * @param id
     * @param status
     */
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }

    /**
     * 查询所有护理计划
     * @param nursingPlan
     * @return
     */
    public List<NursingPlan> list(NursingPlan nursingPlan) {
        List<NursingPlan> nursingPlanList = nursingPlanMapper.list(nursingPlan);
        return nursingPlanList;
    }
}
